﻿/*
 * Created by Wang Lei on 21-07-01.
 * Copyright (c) 2006-2021 Kaitiren
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software
 * in a product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 */

using System;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;


namespace HLLib
{

	/// <summary>
	/// HLStringUtils
	/// </summary>
	public class HLStringUtils
	{
		/// <summary>
		/// strTable
		/// </summary>
		public static string[] strTable = new string[]
		{
		"0",
		"1",
		"2",
		"3",
		"4",
		"5",
		"6",
		"7",
		"8",
		"9",
		"A",
		"B",
		"C",
		"D",
		"E",
		"F"
		};


		/// <summary>
		/// byte 转 UTF-8 字符串.
		/// GetStringFromByteArrayConvertUTF8
		/// </summary>
		/// <param name="_b_array">转换byte数组</param>
		/// <returns></returns>
		public string GetStringFromByteArrayConvertUTF8(byte[] _b_array)
		{
			return System.Text.Encoding.UTF8.GetString(_b_array);
		}


		/// <summary>
		/// 字符串 转 UTF-8 数组.
		/// GetByteArrayFromStringConvertUTF8
		/// </summary>
		/// <param name="_str">转换字符串</param>
		/// <returns></returns>
		public byte[] GetByteArrayFromStringConvertUTF8(string _str)
		{
			return System.Text.Encoding.UTF8.GetBytes(_str);
		}


		/// <summary>
		/// bytes --> string
		/// Bytes2HexString
		/// </summary>
		/// <param name="bytes"></param>
		/// <returns></returns>
		public static string Bytes2HexString(byte bytes)
		{
			string str = string.Empty;
			str += HLStringUtils.strTable[bytes >> 4];
			return str + HLStringUtils.strTable[(int)(bytes & 15)];
		}


		/// <summary>
		/// bytes array --> string
		/// Bytes2HexString
		/// </summary>
		/// <param name="bytes"></param>
		/// <returns></returns>
		public static string Bytes2HexString(byte[] bytes)
		{
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < bytes.Length; i++)
			{
				stringBuilder.Append(HLStringUtils.strTable[bytes[i] >> 4]);
				stringBuilder.Append(HLStringUtils.strTable[(int)(bytes[i] & 15)]);
			}
			return stringBuilder.ToString();
		}


		/// <summary>
		/// EraseZeroTail
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string EraseZeroTail(string str)
		{
			if (str == null)
			{
				Debug.LogError("Error: HLStringUtils:: EraseZeroTail -> null found.");
				return string.Empty;
			}
			int i;
			for (i = 0; i < str.Length; i++)
			{
				if (str[i] == '\0')
				{
					break;
				}
			}
			if (i == 0)
			{
				return string.Empty;
			}
			return str.Substring(0, i);
		}


		/// <summary>
		/// 通过bytearray 获取UTF-8 字符集.
		/// GetStringUtf8
		/// </summary>
		/// <param name="bytes"></param>
		/// <returns>UTF-8 string</returns>
		public static string GetStringUtf8(byte[] bytes)
		{
			string text;
			if (bytes.Length > 3 && bytes[0] == 239 && bytes[1] == 187 && bytes[2] == 191)
			{
				text = Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
			}
			else
			{
				text = Encoding.UTF8.GetString(bytes);
			}
			if (!string.IsNullOrEmpty(text) && text.Length > 0 && text[0] == '﻿')
			{
				text = text.Substring(1);
			}
			return text;
		}


		/// <summary>
		/// GetStringUtf8NoBom
		/// </summary>
		/// <param name="bytes"></param>
		/// <param name="index"></param>
		/// <returns></returns>
		public static string GetStringUtf8NoBom(byte[] bytes, int index)
		{
			return Encoding.UTF8.GetString(bytes, index, bytes.Length - index);
		}


		/// <summary>
		/// GetStringUtf8NoBom
		/// </summary>
		/// <param name="bytes"></param>
		/// <param name="index"></param>
		/// <param name="iLen"></param>
		/// <returns></returns>
		public static string GetStringUtf8NoBom(byte[] bytes, int index, int iLen)
		{
			return Encoding.UTF8.GetString(bytes, index, iLen);
		}


		/// <summary>
		/// HexString2Bytes
		/// </summary>
		/// <param name="strHex"></param>
		/// <returns></returns>
		public static byte[] HexString2Bytes(string strHex)
		{
			int num = strHex.Length / 2;
			byte[] array = new byte[num];
			byte[] array2 = new byte[2];
			for (int i = 0; i < num; i++)
			{
				for (int j = 0; j < 2; j++)
				{
					if (strHex[i * 2 + j] >= '0' && strHex[i * 2 + j] <= '9')
					{
						array2[j] = (byte)(strHex[i * 2 + j] - '0');
					}
					else
					{
						if (strHex[i * 2 + j] >= 'A' && strHex[i * 2 + j] <= 'F')
						{
							array2[j] = (byte)(strHex[i * 2 + j] - 'A' + '\n');
						}
						else
						{
							if (strHex[i * 2 + j] >= 'a' && strHex[i * 2 + j] <= 'f')
							{
								array2[j] = (byte)(strHex[i * 2 + j] - 'a' + '\n');
							}
						}
					}
				}
				array[i] = (byte)((int)array2[0] << 4 | (int)array2[1]);
			}
			return array;
		}


		/// <summary>
		/// String 转 MD5.
		/// StrToMD5
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string StrToMD5(string str)
		{
			MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
			byte[] bytes = Encoding.ASCII.GetBytes(str);
			byte[] array = mD5CryptoServiceProvider.ComputeHash(bytes);
			string text = string.Empty;
			for (int i = 0; i < array.Length; i++)
			{
				text += array[i].ToString("x2");
			}
			return text.ToLower();
		}
	}
}
